package com.ruoyi.web.controller.uav;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.constant.CommonConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.uav.domain.UavTheoreticStudy;
import com.ruoyi.uav.service.IUavTheoreticStudyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

/**
 * 理论学习Controller
 *
 * @author ruoyi
 * @date 2024-01-04
 */
@RestController
@RequestMapping("/uav/study")
@Api(value = "理论学习控制层", tags = "理论学习控制层")
public class UavTheoreticStudyController extends BaseController {
    @Resource
    private IUavTheoreticStudyService uavTheoreticStudyService;

    /**
     * 查询理论学习列表
     */
    @PreAuthorize("@ss.hasPermi('uav:study:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询理论学习列表", notes = "传入uavTheoreticStudy")
    public TableDataInfo list(UavTheoreticStudy uavTheoreticStudy) {
        startPage();
        List<UavTheoreticStudy> list = uavTheoreticStudyService.selectUavTheoreticStudyList(uavTheoreticStudy);
        return getDataTable(list);
    }

    /**
     * 导出理论学习列表
     */
    @PreAuthorize("@ss.hasPermi('uav:study:export')")
    @Log(title = "理论学习", businessType = BusinessType.EXPORT)
    @RepeatSubmit(interval = 2000, message = "请求过于频繁")
    @GetMapping("/export")
    @ApiOperation(value = "导出理论学习列表", notes = "传入uavTheoreticStudy")
    public AjaxResult export(UavTheoreticStudy uavTheoreticStudy) {
        List<UavTheoreticStudy> list = uavTheoreticStudyService.selectUavTheoreticStudyList(uavTheoreticStudy);
        ExcelUtil<UavTheoreticStudy> util = new ExcelUtil<UavTheoreticStudy>(UavTheoreticStudy.class);
        return util.exportExcel(list, "理论学习数据");
    }

    /**
     * 获取理论学习详细信息
     */
    @PreAuthorize("@ss.hasPermi('uav:study:query')")
    @GetMapping(value = "/{theoreticStudyId}")
    @ApiOperation(value = "获取理论学习详细信息", notes = "传入theoreticStudyId")
    public R<UavTheoreticStudy> getInfo(@PathVariable("theoreticStudyId") Long theoreticStudyId) {
        return R.ok(uavTheoreticStudyService.getOne(Wrappers.<UavTheoreticStudy>lambdaQuery().eq(UavTheoreticStudy::getDelFlag,CommonConstants.DEL_FLAG_OK).eq(UavTheoreticStudy::getTheoreticStudyId,theoreticStudyId)));
    }

    /**
     * 新增理论学习
     */
    @PreAuthorize("@ss.hasPermi('uav:study:add')")
    @Log(title = "理论学习", businessType = BusinessType.INSERT)
    @RepeatSubmit(interval = 2000, message = "请求过于频繁")
    @PostMapping
    @ApiOperation(value = "新增理论学习", notes = "传入uavTheoreticStudy")
    public R<Integer> add(@RequestBody UavTheoreticStudy uavTheoreticStudy) {
        uavTheoreticStudy.setCreateBy(SecurityUtils.getUsername());
        uavTheoreticStudy.setCreateTime(DateUtils.getNowDate());
        return R.ok(uavTheoreticStudyService.getBaseMapper().insert(uavTheoreticStudy));
    }

    /**
     * 修改理论学习
     */
    @PreAuthorize("@ss.hasPermi('uav:study:edit')")
    @Log(title = "理论学习", businessType = BusinessType.UPDATE)
    @RepeatSubmit(interval = 2000, message = "请求过于频繁")
    @PutMapping
    @ApiOperation(value = "修改理论学习", notes = "传入uavTheoreticStudy")
    public R<Integer> edit(@RequestBody UavTheoreticStudy uavTheoreticStudy) {
        if (ObjectUtils.isEmpty(uavTheoreticStudy.getStartTime())||ObjectUtils.isEmpty(uavTheoreticStudy.getEndTime())){
            return R.fail("缺少开始时间或结束时间");
        }
        String s = DateUtils.timeDistance(uavTheoreticStudy.getEndTime(), uavTheoreticStudy.getStartTime());
        uavTheoreticStudy.setLearningTime(s);
        return R.ok(uavTheoreticStudyService.getBaseMapper().updateById(uavTheoreticStudy));
    }

    /**
     * 删除理论学习
     */
    @PreAuthorize("@ss.hasPermi('uav:study:remove')")
    @Log(title = "理论学习", businessType = BusinessType.DELETE)
    @RepeatSubmit(interval = 2000, message = "请求过于频繁")
	@DeleteMapping("/{theoreticStudyIds}")
    @ApiOperation(value = "删除理论学习", notes = "传入theoreticStudyIds")
    public R<Integer> remove(@PathVariable Long[] theoreticStudyIds) {
        return R.ok(uavTheoreticStudyService.getBaseMapper().update(null, Wrappers.<UavTheoreticStudy>lambdaUpdate().in(UavTheoreticStudy::getTheoreticStudyId,theoreticStudyIds).eq(UavTheoreticStudy::getDelFlag, CommonConstants.DEL_FLAG_OK).set(UavTheoreticStudy::getDelFlag,CommonConstants.DEL_FLAG_DELETED)));
    }

}


